package  
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	/**
	 * parameters:Object
	 * - displayObject -> DisplayObject where the loader content will be shown
	 * - x: coordinate
	 * - y: coordinate
	 * - rotation: rotation
	 * - alpha: alpha
	 * - scaleX: scaleX
	 * - scaleY: scaleY
	 * - className:String -> Load a specific Linkage class name
	 * - convertToBMP:Boolean -> Converts the entire content into BMP
	 * @author Lorenzo Nuvoletta
	 */
	public class AssetLoader extends Loader
	{
		private var _filename:String;		
		private var _parameters:Object;
		
		public var completed:Boolean;
		public var paramsArr:Array;
		
		public function AssetLoader(filename:String, parameters:Object = null) 
		{
			
			_filename = filename;
			_parameters = parameters;
			
			if (AssetsLibrary.ASSETS.hasOwnProperty(filename)) {
				AssetsLibrary.ASSETS[filename].paramsArr.push(parameters);
				if (AssetsLibrary.ASSETS[filename].completed) {
					AssetsLibrary.ASSETS[filename].parseParameters();
				}
			}
			else {
				paramsArr = new Array();
				AssetsLibrary.ASSETS[filename] = this;
				AssetsLibrary.ASSETS[filename].paramsArr.push(parameters);				
				contentLoaderInfo.addEventListener(Event.COMPLETE, assetLoaded);
				contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, assetError);
				load( new URLRequest( _filename ) );				
			}			
		}
		
		public function parseParameters():void {
			for (var i:int = 0; i < paramsArr.length; i++) {
				var obj:Object = paramsArr[i];
				if (obj) {
					
					var nameOnBMPLibrary:String = _filename + (obj.hasOwnProperty('className')? '.' + obj.className : '');
					var derivate:*;
					
					if ( (obj.hasOwnProperty('convertToBMP') && obj.convertToBMP && !AssetsLibrary.BMPD.hasOwnProperty(nameOnBMPLibrary))
						|| !obj.hasOwnProperty('convertToBMP') || (obj.hasOwnProperty('convertToBMP') && !obj.convertToBMP) ) {
						if (obj.hasOwnProperty('className')) {						
							if (contentLoaderInfo.applicationDomain.hasDefinition(obj.className)) {							
								var className:Class = contentLoaderInfo.applicationDomain.getDefinition(obj.className) as Class;						
								derivate = new className();
							}
							else {
								derivate = new Sprite();
							}
						}
						else {
							derivate = this.content;
						}
					}
					
					if (obj.hasOwnProperty('convertToBMP') && obj.convertToBMP) {
						if (!AssetsLibrary.BMPD.hasOwnProperty(nameOnBMPLibrary)) {
							var bmpd:BitmapDataAdvanced = new BitmapDataAdvanced(derivate.width, derivate.height);							
							var matrix:Matrix = new Matrix();
							var rect:Rectangle = derivate.getBounds(null);
							bmpd.offSet = new Point(rect.x, rect.y);
							matrix.translate(-rect.x, -rect.y);
							bmpd.draw(derivate, matrix);
							AssetsLibrary.BMPD[nameOnBMPLibrary] = bmpd;
						}
						if(obj.hasOwnProperty('displayObject')) derivate = new Bitmap(AssetsLibrary.BMPD[nameOnBMPLibrary]);						
					}					
					
					if(obj.hasOwnProperty('displayObject')){
						obj.displayObject.addChild(derivate);
						
						if (obj.hasOwnProperty('x')) derivate.x = obj.x;
						if (obj.hasOwnProperty('y')) derivate.y = obj.y;
						if (obj.hasOwnProperty('rotation')) derivate.rotation = obj.rotation;
						if (obj.hasOwnProperty('alpha')) derivate.alpha = obj.alpha;
						if (obj.hasOwnProperty('scaleX')) derivate.scaleX = obj.scaleX;
						if (obj.hasOwnProperty('scaleY')) derivate.scaleY = obj.scaleY;
					}
				}
			}
			paramsArr = new Array();
		}
		
		// ERROR -------------------------------------------------------------------------------
		private function assetError( e:IOErrorEvent ):void {			
			trace("Error loading asset: " + e.text);			
			removeListeners();
		}
		
		// LOADED -------------------------------------------------------------------------------
		private function assetLoaded( e:Event ):void {
			trace("Asset Loaded: " + _filename);
			removeListeners();
			completed = true;
			parseParameters();			
		}
		
		private function removeListeners():void {
			contentLoaderInfo.removeEventListener(Event.COMPLETE, assetLoaded);
			contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, assetError);
		}
		
	}

}